package com.keta.foundation.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.keta.foundation.dao.FileResourceDAO;
import com.keta.foundation.entity.FileResource;
import com.keta.foundation.service.FileResourceService;
import com.keta.foundation.util.dwz.Page;
import com.keta.foundation.util.dwz.PageUtil;

@Service
@Transactional
public class FileResourceServiceImpl implements FileResourceService {

	@Autowired
	private FileResourceDAO fileResourceDAO;
	
	@Override
	public FileResource get(Long id) {
		return fileResourceDAO.findOne(id);
	}
	

	@Override
	public long getFileResourceCount() {
		return fileResourceDAO.count();
	}

	@Override
	public void saveOrUpdate(FileResource fileResource) {
		fileResourceDAO.save(fileResource);
	}
	
	@Override
	public List<FileResource> getAll() {
		return fileResourceDAO.findAll();
	}

	@Override
	public List<FileResource> getAll(Page page) {
		org.springframework.data.domain.Page<FileResource> springPage = fileResourceDAO.findAll(PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return springPage.getContent();
	}

	@Override
	public List<FileResource> getAll(Specification<FileResource> specification, Page page) {
		org.springframework.data.domain.Page<FileResource> springPage = fileResourceDAO.findAll(specification,
				PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return springPage.getContent();
	}

	@Override
	public void delete(Long id) {
		fileResourceDAO.delete(id);
	}
	
	@Override
	public void delete(FileResource fileResource) {
		fileResourceDAO.delete(fileResource);
	}
	
	@Override
	public FileResource getByUuid(String uuid){
		return fileResourceDAO.getByUuid(uuid);
	}

}
